<?php

namespace Xp130\Rank\model;

use Exception;
use PDO;
use Xp130\Rank\ConfigLoader;

class BaseModel
{
    protected PDO $pdo;

    public function __construct(){
        try {
            $config = (new ConfigLoader())->load()['mysql'];
            $dsn = sprintf(
                '%s:host=%s;port=%s;dbname=%s;charset=%s',
                $config['driver'],
                $config['host'],
                $config['port'],
                $config['database'],
                $config['charset']
            );
            $this->pdo = new PDO(
                $dsn,
                $config['username'],
                $config['password'],
                [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
            );

        } catch (Exception $e) {
            throw new Exception('数据库连接失败:'.$e->getMessage());
        }
    }

}